/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;

import clases.ClsDatabase;
import clases.ClsMetodos;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Angel
 */
public class dlgPresupuestoPartidaRango extends javax.swing.JDialog {

    private ClsMetodos metodos = new ClsMetodos();
    private ClsDatabase bd = new ClsDatabase();
    DefaultComboBoxModel modelcombo1 = new DefaultComboBoxModel();
    DefaultComboBoxModel modelcombo2 = new DefaultComboBoxModel();
    String idproyecto = "";
    String idpresupuesto = "";
    int opcion_final = 1;
    int a = 1;

    public dlgPresupuestoPartidaRango(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        inicio();

    }

    public dlgPresupuestoPartidaRango(java.awt.Frame parent, boolean modal, String idproyectoc, String idpresupuestoc, int opcion) {
        super(parent, modal);
        initComponents();

        this.idproyecto = idproyectoc;
        this.idpresupuesto = idpresupuestoc;
        opcion_final = opcion;
        inicio();
//        System.out.println("sergiooo "+idproyectoc);
    }

    public void inicio() {
        a = 1;
        setLocationRelativeTo(null);
        metodos._iconJDialog(this);
        carga_item();
        a = 2;
    }

    public void carga_item() {
//        System.out.println("proetctos "+idproyecto);
        String filtro = "";
        if (opcion_final == 1) {
            filtro = "";
        } else {
            filtro = " and p.idpresupuesto ='" + idpresupuesto + "'";
        }
        String sql = "SELECT concat(p.orden,'.', pp.item),concat(p.orden,'.', pp.item,' ',pp.nombre)  FROM presupuesto p,presupuesto_partida pp "
                + "where pp.idproyecto=p.idproyecto and  pp.idpresupuesto=p.idpresupuesto  and pp.tipo='0' and p.idproyecto='" + idproyecto + "' " + filtro + "order by p.orden asc, pp.item asc;";
        metodos.comboboxLlenar(jComboBox1, modelcombo1, sql);
        metodos.comboboxLlenar(jComboBox2, modelcombo2, sql);
    }

    public void reporte() {
        if (jComboBox1.getSelectedIndex() != -1) {
            if (jComboBox2.getSelectedIndex() != -1) {
                String inicio = modelcombo1.getElementAt(jComboBox1.getSelectedIndex()).toString() + "";
                String fin = modelcombo2.getElementAt(jComboBox2.getSelectedIndex()).toString() + "";
                metodos.reporte_estandar("acu_rango.jasper", new String[]{idproyecto, idpresupuesto, inicio, fin}, "Reporte Análisis de costos unitarios", 1080, 650);
            } else {
                javax.swing.JOptionPane.showMessageDialog(this, "Seleccione el final del rango");
            }
        } else {
            javax.swing.JOptionPane.showMessageDialog(this, "Seleccione el inicio del rango");
        }

    }

    public void valida() {
        if (a == 2) {
            if (jComboBox2.getSelectedIndex() != -1 && jComboBox2.getSelectedIndex() != -1) {
                if (jComboBox2.getSelectedIndex() < jComboBox1.getSelectedIndex()) {
                   // jComboBox1.s(false);
                    int s2=jComboBox2.getSelectedIndex();
                    System.out.println(s2);
                    jComboBox2.setSelectedIndex(s2);
                    javax.swing.JOptionPane.showMessageDialog(jComboBox2, "El orden seleccionado de ítems es incorrecto");

                }
            }

        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ACU x rango");

        jPanel1.setBackground(new java.awt.Color(225, 235, 247));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Seleccionar rango de los ACU"));
        jPanel2.setOpaque(false);
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jPanel2.add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 30, 380, -1));

        jLabel1.setText("Desde:");
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, 40, 25));

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox2ItemStateChanged(evt);
            }
        });
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });
        jPanel2.add(jComboBox2, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 70, 380, -1));

        jLabel2.setText("Hasta:");
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 40, 25));

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 470, 110));

        jButton1.setText("Ver");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 140, 80, -1));

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/icon/principales/fondo_dialog.jpg"))); // NOI18N
        jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 450, 330));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (jComboBox2.getSelectedIndex() < jComboBox1.getSelectedIndex()) {
            javax.swing.JOptionPane.showMessageDialog(this, "El orden seleccionado de ítems es incorrecto");
        } else {
            reporte();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
       
    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
//        valida();
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox2ItemStateChanged
//        if(evt.getSource()==jComboBox2){
//            valida();
//        } 
        
    }//GEN-LAST:event_jComboBox2ItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dlgPresupuestoPartidaRango.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dlgPresupuestoPartidaRango.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dlgPresupuestoPartidaRango.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlgPresupuestoPartidaRango.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgPresupuestoPartidaRango dialog = new dlgPresupuestoPartidaRango(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
